package com.xiaochong.bitong.api.schedule;

import com.xc.logclient.utils.LogTrace;
import com.xiaochong.bitong.api.webWs.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.websocket.Session;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description: java类作用描述
 * @Author: org
 * @CreateDate: 2018/8/31 16:43
 */
@Component
public class CleanWebsocketSessionSchedule {

    @Autowired
    private InitCurrencyDataWS initCurrencyDataWs;
    @Autowired
    private InitExchangePairWS initExchangePairWS;
    @Autowired
    private InitExchangePairWS initCurrencyMessageWS;
    @Autowired
    private InitDepthWS initDepthWs;
    @Autowired
    private InitPairWS initPairAppWs;
    @Autowired
    private InitWS initWS;

    @Scheduled(cron="0 0/1 * * * ?")
    public void calculatePairSort() throws Exception {
        LogTrace.beginTrace();
        long start = System.currentTimeMillis();
        ConcurrentHashMap<String, Session> ss = WsSessionContainer.getSessionMap();
        ConcurrentHashMap<String, Long> sessionDatetimeMap = WsSessionContainer.getSessionDatetimeMap();
        if(ss != null && ss.size() > 0){
            for(Map.Entry<String, Session>  tt: ss.entrySet()){
                    try {
                        if(tt == null || tt.getKey() == null || tt.getValue() == null){
                            continue;
                        }
                        String keys = tt.getKey();
                        Session session = tt.getValue();
                        String[] classStr = keys.split(":");
                        String sessionIdKey = classStr[1];
                        long datetime =sessionDatetimeMap.get(session.getId());
                        long nowdate = System.currentTimeMillis();
                        if(nowdate - datetime > 30000){
                            session.close();
                            WsSessionContainer.removeSession(session,sessionIdKey,classStr[2]);
                            LogTrace.infoKvs("清理session-定时任务","sessionId"+tt.getKey());
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                        LogTrace.error("清理session-定时任务-error",e);
                    }
                    /*if("initExchangePairWS".equals(sessionIdKey)){
                        //i*//**//*nitExchangePairWS.close(tt.getValue(),datetime);
                    }else if("initCurrencyDataWs".equals(sessionIdKey)){
                       // session.close();
                    }else if("initCurrencyMessageWS".equals(sessionIdKey)){
                        // initCurrencyMessageWS.close(tt.getValue(),datetime);
                    }else if("initDepthWs".equals(sessionIdKey)){
                        //  initDepthWs.close(tt.getValue(),datetime);
                    }else if("initPairAppWs".equals(sessionIdKey)){
                        //  initPairAppWs.close(tt.getValue(),datetime);
                    }else if("initWS".equals(sessionIdKey)){
                        //  initWS.close(tt.getValue(),datetime);
                    }*/


            }
        }
        LogTrace.info("定时清理ws30秒为请求的链接end","end",String.valueOf(System.currentTimeMillis()-start));
    }
}
